package OutOfLine

import com.alibaba.fastjson.{JSON, JSONArray}
import org.apache.commons.lang.StringUtils
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.SparkSession

object SailAll {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession
      .builder()
      .appName(s"${this.getClass.getName}")
      .master("local[*]")
      .getOrCreate()
    val sc = spark.sparkContext
    val logRDD: RDD[String] = sc.textFile("D:\\asa\\dianshang\\pay - 副本.log")
    val filter = logRDD.map(line => {
      val nObject = JSON.parseObject(line)
      //获取pay_status：1：成功，0：失败
      var pay_status = nObject.getString("pay_status")
      if (StringUtils.isEmpty(pay_status)) {
        pay_status = "0"
      }

      var list = List[Double]()
      var money = "0.0"
      if (line.contains("goods")) {
        val goodsArr: JSONArray = nObject.getJSONArray("goods")
        if (goodsArr.size() > 0) {
          for (o <- 0 until goodsArr.size()) {
            val goodsArrObject = goodsArr.getJSONObject(o)
            //获取money
            var money = goodsArrObject.getString("money")
            if (StringUtils.isEmpty(money)) {
              list :+= 0.0
            } else {
              list :+= money.toDouble
            }

          }
        }

      }
      (pay_status.toInt, list)
    }).filter(x => (x._1 == 1))

    val salALL = filter.map(x=>x._2.sum).sum()
    println(salALL)
    //87000.0

    spark.stop()
  }
}
